﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using SharePointStu.CAMLHelper.CAML;

namespace CAMLHelperTest
{
    [TestClass]
    public class CAMLManagerTest
    {
        /// <summary>
        ///A test for CAMLManager Constructor
        ///</summary>
        [TestMethod()]
        public void CAMLManagerConstructorTest1()
        {
            CAMLManager target = new CAMLManager();
            
            Assert.AreNotEqual(null, target);
        }
        /// <summary>
        ///A test for CAMLManager Constructor
        ///</summary>
        [TestMethod()]
        public void CAMLManagerConstructorTest2()
        {
            QueryGroup queryGroup = new QueryGroup(Types.JoinTypes.And, QueryGroup.MergeTypes.BottomGroup, Types.FieldRefTypes.Name, "field", Types.FieldTypes.Text, Types.QueryTypes.Eq, "test");
            OrderBy order = new OrderBy("field", true, Types.FieldRefTypes.Name);
            CAMLManager target = new CAMLManager(queryGroup, order);

            Assert.AreEqual(target.QueryGroups.Count, 1);
            Assert.AreEqual(target.QueryGroups[0], queryGroup);
            Assert.AreEqual(target.OrderBy.Count, 1);
            Assert.AreEqual(target.OrderBy[0], order);
        }

        /// <summary>
        ///A test for CAMLManager Constructor
        ///</summary>
        [TestMethod()]
        public void CAMLManagerConstructorTest3()
        {
            OrderBy order = new OrderBy("field", true, Types.FieldRefTypes.Name);
            CAMLManager target = new CAMLManager(order);

            Assert.AreEqual(target.OrderBy.Count, 1);
            Assert.AreEqual(target.OrderBy[0], order);
        }

        /// <summary>
        ///A test for GetCAML
        ///</summary>
        [TestMethod()]
        public void GetCAMLTest()
        {
            string fieldYesNoId = Guid.NewGuid().ToString();
            string fieldCounterId = Guid.NewGuid().ToString();
            string fieldYesNo2Id = Guid.NewGuid().ToString();
            string fieldYesNo3Id = Guid.NewGuid().ToString();
            string fieldOrderById = Guid.NewGuid().ToString();
            string fieldViewId = Guid.NewGuid().ToString();

            CAMLManager target = new CAMLManager(new QueryGroup("field", Types.FieldTypes.Text, Types.QueryTypes.Eq, "First"));
            target.QueryGroups.Add(new QueryGroup(Types.FieldRefTypes.ID, fieldYesNoId, Types.FieldTypes.YesNo, Types.QueryTypes.Neq, "true"));
            target.QueryGroups.Add(new QueryGroup(Types.JoinTypes.Or, QueryGroup.MergeTypes.Contain, Types.FieldRefTypes.ID, fieldCounterId, Types.FieldTypes.Counter, Types.QueryTypes.Leq, "99"));
            target.QueryGroups.Add(new QueryGroup(Types.JoinTypes.Or, QueryGroup.MergeTypes.BottomGroup, Types.FieldRefTypes.ID, fieldYesNo2Id, Types.FieldTypes.YesNo, Types.QueryTypes.Eq, "false"));
            target.QueryGroups.Add(new QueryGroup(Types.JoinTypes.Or, QueryGroup.MergeTypes.BottomGroup, Types.FieldRefTypes.Name, "fieldName", Types.FieldTypes.YesNo, Types.QueryTypes.Eq, "true"));
            target.QueryGroups.Add(new QueryGroup(Types.JoinTypes.And, QueryGroup.MergeTypes.BottomGroup, Types.FieldRefTypes.ID, fieldYesNo3Id, Types.FieldTypes.YesNo, Types.QueryTypes.Neq, "false"));

            target.OrderBy.Add(new OrderBy(fieldOrderById, true, Types.FieldRefTypes.ID));
            target.OrderBy.Add(new OrderBy("orderfield2", false));

            target.ViewFields.Add(new ViewField(fieldViewId, Types.FieldRefTypes.ID));
            target.ViewFields.Add(new ViewField("Title"));

            string expected = string.Format("<Where><Or><Or><And><Eq><FieldRef Name=\"field\" /><Value Type=\"Text\">First</Value></Eq><Neq><FieldRef ID=\"{0}\" /><Value Type=\"YesNo\">true</Value></Neq></And><Leq><FieldRef ID=\"{1}\" /><Value Type=\"Counter\">99</Value></Leq></Or><And><Or><Eq><FieldRef ID=\"{2}\" /><Value Type=\"YesNo\">false</Value></Eq><Eq><FieldRef Name=\"fieldName\" /><Value Type=\"YesNo\">true</Value></Eq></Or><Neq><FieldRef ID=\"{3}\" /><Value Type=\"YesNo\">false</Value></Neq></And></Or></Where><OrderBy><FieldRef ID=\"{4}\" Ascending=\"True\" /><FieldRef Name=\"orderfield2\" Ascending=\"False\" /></OrderBy><ViewFields><FieldRef ID=\"{5}\" /><FieldRef Name=\"Title\" /></ViewFields>",
                                            fieldYesNoId, fieldCounterId, fieldYesNo2Id, fieldYesNo3Id, fieldOrderById, fieldViewId);
            string actual = target.GetFullCAML();

            Assert.AreEqual(expected, actual);
        }
    }
}
